﻿using System.Linq;
using Identity.Mapping;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Identity
{
    public abstract class MemberDbContextBase : IdentityDbContext<User, Role, string>
    {
        protected MemberDbContextBase()
        {
            //this.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
        }

        protected MemberDbContextBase(DbContextOptions options) : base(options)
        {
            // this.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
        }

        public IQueryable<Vendor> Vendors => Set<Vendor>();
        public IQueryable<District> Districts => Set<District>();

        protected override void OnModelCreating(ModelBuilder build)
        {
            base.OnModelCreating(build);
            //改变一下table的名字
            build.Entity<User>(op =>
            {
                op.Property(_ => _.Id).HasMaxLength(36);
                op.Property(_ => _.Name).HasMaxLength(36);
                op.HasOne(f => f.District);
                op.HasOne(f => f.Vendor);
                op.ToTable("member_user");
            });
            build.Entity<Role>(op =>
            {
                op.ToTable("member_role");
                op.Property(_ => _.Id).HasMaxLength(36);
                op.Property(f => f.Description).HasMaxLength(100);
            });
            build.Entity<IdentityUserClaim<string>>(op =>
            {
                op.ToTable("member_user_claim");
                op.Property(_ => _.Id).HasMaxLength(36);
            });
            build.Entity<IdentityRoleClaim<string>>(op =>
            {
                op.ToTable("member_role_claim");
                op.Property(_ => _.Id).HasMaxLength(36);
            });
            build.Entity<IdentityUserRole<string>>(op => { op.ToTable("member_user_role"); });
            build.Entity<IdentityUserLogin<string>>(op =>
            {
                op.ToTable("member_user_login");
                op.Property(_ => _.UserId).HasMaxLength(36);
                op.Property(_ => _.LoginProvider).HasMaxLength(128);
                op.Property(_ => _.ProviderKey).HasMaxLength(128);
                op.Property(_ => _.ProviderDisplayName).HasMaxLength(512);
            });

            build.Entity<IdentityUserToken<string>>(op =>
            {
                op.ToTable("member_user_token");
                op.Property(f => f.UserId).HasMaxLength(36);
                op.Property(_ => _.LoginProvider).HasMaxLength(128);
                op.Property(_ => _.Name).HasMaxLength(128);
            });

            build.ApplyConfiguration(new DistrictMapping());

            //兼容mysql
            build.Entity<User>(i =>
            {
                i.Property(o => o.EmailConfirmed).HasConversion<int>();
                i.Property(o => o.LockoutEnabled).HasConversion<int>();
                i.Property(o => o.PhoneNumberConfirmed).HasConversion<int>();
                i.Property(o => o.TwoFactorEnabled).HasConversion<int>();
            });
            build.ApplyConfiguration(new VendorMapping());
        }
    }
}